package com.yunshang.shoppingmall.Wechat;

import com.yunshang.shoppingmall.common.LayUIUtil;
import com.yunshang.shoppingmall.entity.WeChatArticle;
import com.yunshang.shoppingmall.service.WeChat.WeChatArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RequestMapping(value = "/WeChat")
@Controller
public class weChatArticleController {

    @Autowired
    WeChatArticleService weChatArticleService;

    //添加页面
    @RequestMapping("/Article")
    public ModelAndView Article() {
        ModelAndView modelAndView = new ModelAndView("WeChat/Article/List");
        return modelAndView;
    }

    //列表Json数据
    @ResponseBody
    @RequestMapping("/JsonArticleList")
    public LayUIUtil JsonArticleList() {
        List<WeChatArticle> muList=weChatArticleService.selectArticleList();
        if(muList!=null) {
            return LayUIUtil.data(muList.size(), muList);
        }
        else
        {
            return LayUIUtil.data(0, null);
        }
    }

    //添加页面
    @RequestMapping("/Article/Add")
    public ModelAndView Add() {
        ModelAndView modelAndView = new ModelAndView("WeChat/Article/Add");
        return modelAndView;
    }

    //添加页面
    @ResponseBody
    @RequestMapping("/Article/AddArticle")
    public int AddArticle(@RequestParam Map<String,String> map, HttpServletRequest request) {
        String title = map.get("title");
        String description = map.get("description");
        String picUrl = map.get("picUrl");
        String url = map.get("url");
        String sort1 = map.get("sort");
        int sort = Integer.parseInt(sort1);
        WeChatArticle weChatArticle = new WeChatArticle();
        weChatArticle.setTitle(title);
        weChatArticle.setDescription(description);
        weChatArticle.setPicurl(picUrl);
        weChatArticle.setUrl(url);
        weChatArticle.setSort(sort);
        int row = weChatArticleService.insertSelective(weChatArticle);
        return row;
    }

    //编辑页面
    @RequestMapping("/Article/Edit")
    public ModelAndView Edit(@RequestParam Map<String,String> map, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("WeChat/Article/Edit");
        String id1 = map.get("id");
        int id = Integer.parseInt(id1);
        WeChatArticle weChatArticle = weChatArticleService.selectByPrimaryKey(id);
        if(weChatArticle!=null) {
            modelAndView.addObject("weChatArticle", weChatArticle);
        }
        return modelAndView;
    }

    //添加页面
    @ResponseBody
    @RequestMapping("/Article/EditArticle")
    public int EditArticle(@RequestParam Map<String,String> map, HttpServletRequest request) {
        String id1 = map.get("id");
        int id = Integer.parseInt(id1);
        String title = map.get("title");
        String description = map.get("description");
        String picUrl = map.get("picUrl");
        String url = map.get("url");
        String sort1 = map.get("sort");
        int sort = Integer.parseInt(sort1);
        WeChatArticle weChatArticle = new WeChatArticle();
        weChatArticle.setId(id);
        weChatArticle.setTitle(title);
        weChatArticle.setDescription(description);
        weChatArticle.setPicurl(picUrl);
        weChatArticle.setUrl(url);
        weChatArticle.setSort(sort);
        weChatArticle.setSort(sort);
        int row = weChatArticleService.updateByPrimaryKeySelective(weChatArticle);
        return row;
    }

}
